{#
 # @param menu the category tree, expressed as a list of items. every item has: id, title, href, progress?, children?, active. if present, children is another array of items.
 #}


{##
 # displays progressbar style depending on the actual progress
 #
 # @param number percent_done
 #}
{% macro progressbar_style(percent_done) %}
{{ percent_done >= 70 ? "high" : percent_done > 30 ? "medium" : "low" }}
{% endmacro %}

{#
 # @param number progress the current progressbar value (an integer in 0-100)
 # @param string|null class additional progress bar class
 #}
{% macro progressbar(progress, class) %}
    <span class="progressbar-{{ class|default(_self.progressbar_style(progress)) }}"
          title="{{ "%.0f" | format(progress) }}%">
      <span class="done" style="width: {{ "%.0f%%;" | format(progress) }}"></span>
    </span>
{% endmacro %}

{#
 # @param tree a list of menu items to display
 # @param level the current depth in the menu tree (starting from 0 for top-level items)
 #}
{% macro menu_tree(tree, level, only_expanded) %}
    <ul class="menu-level-{{ level }}">
        {% for item in tree %}
            {% if not only_expanded or item.expanded %}
              {# Warning: "item.children is empty" does not work, see
              https://github.com/fabpot/Twig/issues/307 #}
              <li class="menu-level-{{ level }} {{ item.children | length > 0 ? 'inner' : 'leaf' }}{{ item.expanded ? ' expanded' : '' }}{{ item.active ? ' active' : '' }}">
                  {# TODO: necheme do titlu dať miesto percent konkrétny zlomok "answers/questions"? #}
                  <a class="menu-level-{{ level }} {{ item.children | length > 0 ? 'inner' : 'leaf' }}{{ item.expanded ? ' expanded' : '' }}{{ item.active ? ' active' : '' }}" href="{{ item.href }}">
                    {%- if item.progressbar.progressTotal > 0 -%}
                      {{ _self.progressbar(item.progressbar.percentDone) }}
                    {%- endif -%}{{ item.title }}{% if item.lecturer or item.trainer %}
                      ({% if item.lecturer %}<abbr title="Prednášajúci">P</abbr>{% endif %}{% if item.lecturer and item.trainer %},{% endif %}{% if item.trainer %}<abbr title="Cvičiaci">C</abbr>{% endif %})
                    {% endif %}</a>
                  {% if item.progressbar.progressTotal > 0 %}
                    <span class="for-screen-readers">{{ "%.0f%%" | format(item.progressbar.percentDone) }}</span>
                  {% endif %}
                  {% if item.expanded and item.children is not empty %}
                      {{ _self.menu_tree(item.children, level + 1, item.only_expanded) }}
                  {% endif %}
              </li>
            {% endif %}
        {% endfor %}
    </ul>
{% endmacro %}

<h2 class="for-screen-readers">Kategórie</h2>
<div class="menu">
    {{ _self.menu_tree(menu, 0, false) }}
</div>
{# TODO: niekde možno chceme zobraziť celkový progress #}
